const {
  $Toast
} = require('../../component/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    money: 0,
    bindId: -1,
    bind_id: '',
    list: [],
    isLuckly: true,
    luckyInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      orderId,
      money
    } = options
    this.setData({
      orderId,
      money
    })
    wx.$http().request({
      url: `/AppMaster/Raffle/getLuckyChance?orders_id=${orderId}`,
      data: {},
      method: 'GET'
    }).then(res => {
      const {
        _data
      } = res
      const {
        data,
        message
      } = _data
      if (!data) {
        this.setData({
          isLuckly: false
        })
        return
      }
      let {
        awards,
        bind_id,
        lucky_info
      } = data
      let result = awards
      if (awards.length < 8) {
        result = []
        for (let i = 0; i < 8; i++) {
          if (i % 2 === 0) {
            const items = awards.splice(0, 1)
            if (items.length > 0) {
              let obj = Object.assign({}, {
                opa: .9
              }, items[0])
              result.push(obj)
            } else {
              result.push({
                prize_id: 't' + i,
                prize_name: '谢谢参与',
                thing_name: '',
                opa: .9,
                prize_picture: '../../asset/icon/tks.png'
              })
            }
          } else {
            result.push({
              prize_id: 't' + i,
              prize_name: '谢谢参与',
              thing_name: '',
              opa: .9,
              prize_picture: '../../asset/icon/tks.png'
            })
          }
        }
        this.setData({
          list: result,
          bind_id,
          luckyInfo: lucky_info
        })
      }

    })
  },

  onShow() {
    getApp().getSaleMode()
  },
  bindLottery(bid) {
    return wx.$http().request({
      url: `/AppMaster/Raffle/orderLuckyDraw&bind_id=${bid}`,
      data: {},
      method: 'GET'
    }).then(res => {
      return res._data
    })
  },
  goRoute(e) {
    const {
      dataset
    } = e.currentTarget
    const {
      index
    } = dataset
    if (index * 1 === 1) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      const {
        orderId
      } = this.data
      wx.navigateTo({
        url: '/pages/order/orderDetail/orderDetail?itemId=' + orderId
      })
    }
  },
  startLuckly() {
    let {
      bind_id,
      list
    } = this.data
    this.bindLottery(bind_id).then(res => {
      const {
        data,
        message
      } = res
      if (!data.winning) {
        $Toast({
          content: message,
          type: 'error'
        });
      } else {
        const {
          winning
        } = data
        const {
          prize_id
        } = winning
        let index = list.findIndex(item => item.prize_id === prize_id)
        index = index < 0 ? list.findIndex(item => item.prize_name === '谢谢参与') : index
        this.setData({
          bindId: index
        })
      }
    })
  },
  readRule() {
    const {
      luckyInfo
    } = this.data
    wx.setStorage({
      key: 'luckyInfo',
      data: luckyInfo,
      success: _ => {
        wx.navigateTo({
          url: '/subPage/lotteryRule/index',
        })
      }
    })
  },
  viewOrder() {
    const {
      orderId
    } = this.data
    wx.navigateTo({
      url: '/pages/order/orderDetail/orderDetail?itemId=' + orderId
    })
  }
})